package com.sjz.han.industry.activites;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sjz.han.industry.R;
import com.sjz.han.industry.adapters.MyIntegralAdapter;
import com.sjz.han.industry.adapters.WaitJhMemberAdapter;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.GongLueEntity;
import com.sjz.han.industry.entities.JFRecordEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.PicUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

//我的积分
public class MyIntegralActivity extends BaseActivity implements View.OnClickListener{
    private String myjf;
    private RefreshLayout smartrefresh;
    private RecyclerView action_recy;
    private MyIntegralAdapter integralAdapter;
    private List<JFRecordEntity.JFRecordData.JFRecord> jfListDatas=new ArrayList<>();
    private int pageNum = 1;
    private String title="";
    private boolean isFirst = true;

    private ImageView jf_photo_img;
    private TextView jf_value_tv,jf_dataempty;
    private TextView jf_djsuan_tv,jf_oversuan_tv;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_myintegral;
    }

    @Override
    protected void initDatas() {
        myjf=getIntent().getStringExtra("myjf");
        jf_value_tv.setText(myjf);
        getJFJL(String.valueOf(pageNum));
    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("我的积分");
        jf_dataempty=(TextView)this.findViewById(R.id.jf_dataempty);

        jf_photo_img=(ImageView)this.findViewById(R.id.jf_photo_img);
        PicUtils.showImageViewToCircle(this, R.mipmap.ic_wd_tx, ReqestUrl.BASE+shareUtils.getUserPhoto(), jf_photo_img);

        jf_value_tv=(TextView)this.findViewById(R.id.jf_value_tv);
        smartrefresh = (RefreshLayout) this.findViewById(R.id.common_smartrefresh);
        action_recy = (RecyclerView) this.findViewById(R.id.common_recy);
        action_recy.setLayoutManager(new LinearLayoutManager(this));
        integralAdapter= new MyIntegralAdapter(this);
        action_recy.setAdapter(integralAdapter);

        jf_djsuan_tv=(TextView)this.findViewById(R.id.jf_djsuan_tv);
                jf_oversuan_tv=(TextView)this.findViewById(R.id.jf_oversuan_tv);

    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
        smartrefresh.setOnRefreshListener(headResh);
        smartrefresh.setOnLoadmoreListener(footerResh);
    }

    @Override
    protected void onRetry() {
        pageNum=1;
        getJFJL(String.valueOf(pageNum));
    }
    public OnRefreshListener headResh = new OnRefreshListener() {
        @Override
        public void onRefresh(RefreshLayout refreshlayout) {
            isFirst = false;
            smartrefresh.resetNoMoreData();
            pageNum = 1;
            getJFJL(String.valueOf(pageNum));
        }
    };

    public OnLoadmoreListener footerResh = new OnLoadmoreListener() {
        @Override
        public void onLoadmore(RefreshLayout refreshlayout) {
            isFirst = false;
            if (pageNum == 1 && jfListDatas.size() < ReqestUrl.PAGESIZE) {
                smartrefresh.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
            } else {
                pageNum++;
                getJFJL(String.valueOf(pageNum));
            }
        }
    };
    @Override
    public void onClick(View v) {
     switch (v.getId()){
         case R.id.reback_btn:
             finish();
             break;
     }
    }

    //获取
    public void getJFJL(String page){
        if(isFirst){
            showLoading();
        }
        OkHttpUtils
                .post()
                .url(ReqestUrl.JFRECORD_URL)
                .addParams("token",shareUtils.getToken())
                .addParams("pageSize",String.valueOf(ReqestUrl.PAGESIZE))
                .addParams("page",page)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if(!isFirst){
                    smartrefresh.finishLoadmore();
                    smartrefresh.finishRefresh();
                }
                showFaild();
            }
            @Override
            public void onResponse(String response, int id) {
                 showSuccess();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    isFirst=false;
                    JFRecordEntity entity= JsonUtils.getObject(response,JFRecordEntity.class);
                    if(entity!=null&&entity.code==0&&entity.data.CUserMoneyLogs!=null&&entity.data.CUserMoneyLogs.size()>0){
                        jf_djsuan_tv.setText(entity.data.noRepayMoney+"");
                        jf_oversuan_tv.setText(entity.data.repayMoney+"");
                        jf_dataempty.setVisibility(View.GONE);
                        setAdapterDatas(entity.data.CUserMoneyLogs);

                    }else{
                        if(pageNum==1||jfListDatas.size()==0){
                            jf_dataempty.setVisibility(View.VISIBLE);
                        }
                        smartrefresh.finishRefresh();
                        smartrefresh.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件

//                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
    public void setAdapterDatas(List<JFRecordEntity.JFRecordData.JFRecord> datas){
        smartrefresh.finishLoadmore();
        smartrefresh.finishRefresh();
        if(pageNum==1){
            jfListDatas.clear();
        }
        for(JFRecordEntity.JFRecordData.JFRecord entity:datas){
            jfListDatas.add(entity);
        }
        integralAdapter.setAdapterDatas(jfListDatas);
    }
}
